-- PushM
-- create By cheny3
-- 推送设置

module("PushM", package.seeall);

local pushType = {};
local pushTypeElse = {}; -- 去掉special
local pushSet = {};
local pushContent = {};
local pushTitle = {};
local pushInfo = {};

-- 所有子规则
local rules = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return; end
    _init = true;

    -- 清除
    pushType = {};
    pushTypeElse = {};
    pushContent = {};
    pushTitle = {};

    -- 载入配置表
    local push_CSV = get_push_CSV();

    -- 按优先级排序
    table.sort(push_CSV, sortSetting);

    -- 推送类型
    for i = 1, #push_CSV do
        if push_CSV[i]["type"] ~= "special" then
            -- 特殊不可设置，此处去掉
            table.insert(pushTypeElse, push_CSV[i]["type"]);
        end
        table.insert(pushType, push_CSV[i]["type"]);
        pushContent[push_CSV[i]["type"]] = push_CSV[i]["msg"];
        pushTitle[push_CSV[i]["type"]] = push_CSV[i]["title"];
        pushInfo[push_CSV[i]["type"]] = push_CSV[i];
    end
end

-- 初始化模块
function init()
    -- 清除
    pushSet = {};

    loadCsv();

    -- 载入所有的规则处理子模块
    rules = LOAD_PATH("game/logic/module/push");
end

-- 获取子规则
function queryRule(rule)
    if rules[rule] == "TBL" then
        rules[rule] = LOAD_RUN("game/logic/module/push/" .. rule);
    end

    return rules[rule];
end

-- 根据类型进行索引
function queryType(type, path)
    local m = pushInfo[type];
    if not m or path == "" or not path then
        return m;
    else
        return m[path];
    end
end

-- 按优先级排序
function sortSetting(info1, info2)
    return info1.priority < info2.priority;
end

-- 更新推送设置
function updatePushSetting(pushSetting)
    pushSet = pushSetting;

    -- 抛出事件
    EventMgr.fire(event.UPDATE_PUSH_SETTING);
end

-- 获取推送设置
function getPushSetting()
    return pushSet;
end

-- 设置成默认值
function setPushDefaul()
    for i = 1, #pushType do
        pushSet[pushType[i]] = 1;
    end
end

-- 是否和原本设置相同
function ifChangeSetting(inputSetting)
    for key, value in pairs(inputSetting) do
        if (pushSet[key] ~= value) then
            return true;
        end
    end
    return false;
end

-- 获取推送类型
function getPushType()
    return pushType;
end

-- 获取推送类型（无特殊）
function getPushTypeElse()
    return pushTypeElse;
end

-- 获取玩家当前设置推送的所有内容
function getUserAllPush()
    local userPushInfo = {};
    for pushType, m in pairs(pushSet) do
        local leftTime = 0;
        local content  = pushContent[pushType];
        local title    = pushTitle[pushType];

        -- 设置需要推送的
        if m == 1 and pushType ~= "special" then
            -- 获取子规则
            local f = queryRule(pushType);
            if not f then
                assert(false, string.format("子模块%s不存在。", pushType));
            end

            -- 调用子规则的接口获取推送时间
            if f.getLeftTime then
                leftTime = f.getLeftTime();
            end

            leftTime = math.ceil(leftTime or 0);

            if leftTime > 0 then
                local pushInfo = { ["title"] = title, ["content"] = content, ["leftTime"] = leftTime or 0};
                -- 有剩余时间的
                table.insert(userPushInfo, pushInfo);
            end
        end
    end
    
    return userPushInfo;
end
